package com.nanbei.dp;

import java.util.Arrays;

/**
 * @ClassDescription:  两个数组中 公共的 、长度最长的子数组的长度  （动态规划）  最长公共子串
 * @JdkVersion: 1.8
 * @Author: libs
 * @Created: 2024/7/9 17:30
 */
public class LCSubstring {

     /*
            i   t   h   e   i   m   a
         t  0   1   0   0   0   0   0
         h  0   0   2   0   0   0   0
         e  0   0   0   3   0   0   0
         n  0   0   0   0   0   0   0
     */

    public static void main(String[] args) {
        System.out.println(lcs("itheima", "then"));
    }

    private static int lcs(String itheima, String then) {
        int[][] dp = new int[then.length()][itheima.length()];
        for (int i = 0; i < then.length(); i++) {
            char c = then.charAt(i);
            for (int j = 0; j < itheima.length(); j++) {
                char cha = itheima.charAt(j);
                // 如果两个字母相等就标记为1
                // 如果[i-1][j-1]不为0就证明上一个字母也相等 那就用[i-1][j-1] +1
                if (c == cha){
                    if (i ==0 || j ==0){
                        dp[i][j] = 1;
                    }else {
                        dp[i][j] = dp[i-1][j-1] +1;
                    }
                }else {
                    dp[i][j] = 0;
                }
            }
            System.out.println(Arrays.deepToString(dp));
        }
        int max = 0;
        for (int i = 0; i < then.length(); i++) {
            for (int j = 0; j < itheima.length(); j++) {
                max = Math.max(max,dp[i][j]);
            }
        }

        return max;
    }



}
